<?php
/**
 * Created by PhpStorm.
 * User: lili
 * Date: 2018/11/16
 * Time: 20:42
 */

namespace app\index\model;


use think\Model;
use traits\model\SoftDelete;

class Beginningtreatment extends Model
{
    use SoftDelete;
    protected $deleteTime = 'delete_time';
    protected $autoWriteTimestamp = true;


    public function user()
    {
        return $this->hasOne('User','uid');
    }


    /*
     * 是否存在条件的初始治疗方案
     * @param array $where 搜索数据库的条件
     *
     *
     * */
    public function isHaving($where=''){
        if (empty($where)){
            return false;
        }else{
            $res = $this->where($where)->find();
            if (empty($res)){
                return false;
            }else{
                return true;
            }
        }
        
    }


    /*
    * 获得总数
    * 
    * */    
    public function getCount(){
        $count = $this->count();
         
        return $count;
    }

    /*
    * 获得总数(条件)
    * 
    * */
    public function getBTsCount($w=''){

        if (empty($w)){
            $count = $this
                ->alias('b')
                ->join('__USER__ u ','b.uid = u.uid')
                ->field('u.username,b.*')
                ->count();
        }else{

            $count = $this
                ->alias('b')
                ->join('__USER__ u ','b.uid = u.uid')
                ->field('u.username,b.*')
                ->where($w)
                ->count();


//            dump($this->getLastSql());
        }

        return $count;
    }
    
    /*
     * 获得所有的治疗方案
     * @param $page 页码
     * @param $limit 每页数量
     * @param $w 查询条件
     * */
    public function getAllBTs($page=1,$limit=10,$w=''){


        $start = ($page-1)*$limit;
        if (empty($w)){
            $bts = $this
                ->alias('b')
                ->join('__USER__ u ','b.uid = u.uid')
                ->field('u.username,b.*')
                ->limit($start,$limit)
                ->select();
        }else{

            $bts = $this
                ->alias('b')
                ->join('__USER__ u ','b.uid = u.uid')
                ->field('u.username,b.*')
                ->where($w)
                ->limit($start,$limit)
                ->select();      
            

//            dump($this->getLastSql());
        }


        return $bts;
    }



    /*
     * 通过uid获得起始治疗方案
     * @param int $uid 用户id
     * @param string $sdatetime 时间戳
     *
     * */
    public function getBTByUid($uid,$sdatetime=''){
        
        if (empty($sdatetime)){
            $bt =  $this->where('uid',$uid)->find();
        }else{
            $bt =  $this->where('uid',$uid)->find();
            $bimodel = new Baseinsulin();
            $bi = $bimodel->getbaseinsulin($uid,$sdatetime);
            $stmodel = new Sulintime();
            $st = $stmodel->getSulintime($uid,$sdatetime);
            if ($bi!=false){
                $bt['baseinsulin'] = $bi['baseinsulin'];//如果基础率总量记录表有记录，更新
            }

            if ($st!=false){
                $bt['sulin0003'] = $st['sulin0003'];//如果记录表有记录，更新
                $bt['sulin0307'] = $st['sulin0307'];//如果记录表有记录，更新
                $bt['sulin0712'] = $st['sulin0712'];//如果记录表有记录，更新
                $bt['sulin1217'] = $st['sulin1217'];//如果记录表有记录，更新
                $bt['sulin1722'] = $st['sulin1722'];//如果记录表有记录，更新
                $bt['sulin2224'] = $st['sulin2224'];//如果记录表有记录，更新
            }

        }
        
        
        return $bt;
    }




}